<template>
  <div class="app-container">
    <el-card>
      <!--工具栏-->
      <div class="head-container">
        <eHeader :permission="permission"/>
        <crudOperation :permission="permission" />
      </div>
      <!--工具栏-->

      <!--表单组件
      <eForm ref="form" />-->
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="star" label="评分" />
        <el-table-column prop="teacherId" label="教练编号" />
        <el-table-column prop="teacherName" label="教练名称"/>
        <el-table-column prop="studentName" label="学员" />
        <el-table-column prop="addTime" label="评价时间" />
        <el-table-column prop="description" label="评价内容"/>

        <el-table-column  label="操作" width="150px" align="center">
            <template slot-scope="scope">
              <udOperation
                :data="scope.row"
                :permission="permission"
              />

            </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
    </el-card>

    <el-dialog
      append-to-body
      :close-on-click-modal="false"
      :before-close="crud.cancelCU"
      :visible="crud.status.cu > 0"
      :title="crud.status.title"
      width="500px"
    >
      <el-form
        ref="form"
        :model="form"
        size="small"
        label-width="80px"
      >

        <el-form-item
          label="教练"
          prop="teacherId"
        >
          <el-select v-model="form.teacherId" style="width: 178px" placeholder="请先选择教练">
            <el-option
              v-for="(item, index) in teacherList"
              :key="item.nickname + index"
              :label="item.nickname"
              :value="item.id"
            />
          </el-select>
        </el-form-item>

        <el-form-item
          label="内容"
          prop="description"
        >
          <el-input v-model="form.description" style="width: 370px;"/>
        </el-form-item>


        <el-form-item label="评分" prop="star">
          <el-radio-group v-model="form.star" style="width: 178px">
            <el-radio :label="1">一颗星</el-radio>
            <el-radio :label="2">二颗星</el-radio>
            <el-radio :label="3">三颗星</el-radio>
            <el-radio :label="4">四颗星</el-radio>
            <el-radio :label="5">五颗星</el-radio>
          </el-radio-group>
        </el-form-item>

      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="text"
          @click="crud.cancelCU"
        >
          取消
        </el-button>
        <el-button
          :loading="crud.cu === 2"
          type="primary"
          @click="crud.submitCU"
        >
          确认
        </el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import crudStar from '@/api/ceamStar'
import { cancelOrder } from '@/api/ceamStar'
import eForm from './form'
import eHeader from './header'
import CRUD, { presenter, form } from '@crud/crud'
import crudOperation from '@crud/CRUD.operation'
import pagination from '@crud/Pagination'
import udOperation from '@crud/UD.operation'
import {getTeacherList} from '../../../api/ceamTeacher'

// crud交由presenter持有
const crud = CRUD({
  title: '评价',
  url: 'api/star',
  sort: ['sort,asc', 'id,desc'],
  crudMethod: { ...crudStar }
})
const defaultForm = { teacherId: null, description: '', star: null  }
export default {
  components: { eHeader, eForm, crudOperation, pagination, udOperation },
  mixins: [presenter(crud),form(defaultForm)],
  data() {
    return {
      delLoading: false,
      teacherList: [],
      permission: {
        add: ['admin', 'star:add'],
        edit: ['admin', 'star:edit'],
        del: ['admin', 'star:del']
      }
    }
  },
  created() {
    this.$nextTick(() => {
      //this.init()
    })
  },
  methods: {
    checkPermission,
    // 新增与编辑前做的操作
    [CRUD.HOOK.afterToCU](crud, form) {
      this.listTeacher();
    },
    listTeacher() {
      const params = {
        page: 0,
        size: 100
      }
      getTeacherList(params).then(res => {
        this.teacherList = res.records;
      })
    },
    subDelete(id) {
      this.delLoading = true
      del(id).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.dleChangePage()
        this.init()
        this.$notify({
          title: '删除成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err.response.data.message)
      })
    },
    cancel(data) {
      cancelOrder(data).then(res => {
        this.$notify({
          title: '取消成功',
          type: 'success',
          duration: 2500
        })
      })
        .catch(error => {
          this.$notify({
            title: '取消失败',
            type: 'danger',
            duration: 2500
          })
        })
    },
    add() {
      this.isAdd = true
      this.$refs.form.dialog = true
    },
    edit(data) {
      this.isAdd = false
      const _this = this.$refs.form
      _this.form = {
        id: data.id,
        routeCn: data.routeCn,
        name: data.name,
        startPoint: data.startPoint,
        endPoint: data.endPoint,
        description: data.description
      }
      _this.dialog = true
    }
  }
}
</script>

<style scoped>

</style>
